import json

def load_data_from_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data

def find_recent_three_day_decline(data):
    stock_prices = {}

    # 组织数据，以股票代码为键，存储每个日期的收盘价
    for day in data:
        date = day["trandt"]
        for stock in day["data"]["diff"]:
            stock_code = stock["f12"]
            close_price = stock["f2"]

            if stock_code not in stock_prices:
                stock_prices[stock_code] = []

            stock_prices[stock_code].append((date, close_price))

    # 按时间排序股票价格
    for prices in stock_prices.values():
        prices.sort(key=lambda x: x[0], reverse=True)

    # 找出连跌三天的股票
    declining_stocks = []
    for stock_code, prices in stock_prices.items():
        if len(prices) >= 3:
            for i in range(len(prices) - 2):
                if prices[i][1] > prices[i + 1][1] > prices[i + 2][1]:
                    declining_stocks.append({
                        "stock_code": stock_code,
                        "dates": [prices[i][0], prices[i + 1][0], prices[i + 2][0]],
                        "prices": [prices[i][1], prices[i + 1][1], prices[i + 2][1]]
                    })
                    break

    return declining_stocks

# 读取数据文件
file_path = 'D:\\PythonWorks\\pythonlearning\\01test\\tiantianjijin\\all_processed_data.json'
data = load_data_from_file(file_path)

# 查找最新连跌三天的股票
recent_three_day_declines = find_recent_three_day_decline(data)

# 输出结果
for decline in recent_three_day_declines:
    print(f"股票代码: {decline['stock_code']}")
    print(f"日期: {decline['dates']}")
    print(f"价格: {decline['prices']}")
    print()
